package org.jeecg.modules.appuser.api.person;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.appuser.constant.TaskNamesConstant;
import org.jeecg.modules.appuser.entity.*;
import org.jeecg.modules.appuser.service.*;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "app个人中心接口")
@RestController
@RequestMapping("/app/person/api")
public class PersonCenterContrller {

    @Autowired
    private ITbMnUserService tbMnUserService;

    @Autowired
    private ITbUserAlipayService tbUserAlipayService;

    @Autowired
    private ITbUserCashService tbUserCashService;

    @Autowired
    private ITbUserNoticeStatusService tbUserNoticeStatusService;


    @Autowired
    private ITbUserGameBuidingService tbUserGameBuidingService;

    @Autowired
    private ITbUserWalletService tbUserWalletService;

    @Autowired
    private ITbMnCollectService tbMnCollectService;

    @Autowired
    private ITbUserSqtDetailService tbUserSqtDetailService;

    @Autowired
    private ITbUserSearchHistoryService tbUserSearchHistoryService;

    @Autowired
    private ITbUserNoticeService tbUserNoticeService;

    @Autowired
    private ITbUserSqtService tbUserSqtService;

    @Autowired
    private ITbUserAlipayPwdService tbUserAlipayPwdService;

    @Autowired
    private ITbUserIntegralService tbUserIntegralService;

    @Autowired
    private ITbUserIntegralDetailService tbUserIntegralDetailService;

    //我的邀请界面
    @AutoLog(value = "邀请界面")
    @ApiOperation("邀请界面")
    @PostMapping(value = "/getInvitedData")
    public Result<?> getInvitedData(String userId) {

        return tbMnUserService.getInvitedData(userId);
    }


    @AutoLog(value = "支付宝绑定")
    @ApiOperation("支付宝绑定")
    @PostMapping(value = "/cashAlipayBinding")
    public Result<?> cashAlipayBinding(TbUserAlipay tbUserAlipay) {

        return tbUserAlipayService.cashAlipayBinding(tbUserAlipay);
    }

    @AutoLog(value = "支付宝解绑")
    @ApiOperation("支付宝解绑")
    @PostMapping(value = "/uncashAlipayBinding")
    public Object uncashAlipayBinding(String user_id) {

        return tbUserAlipayService.uncashAlipayBinding(user_id);
    }

    @AutoLog(value = "提现")
    @ApiOperation("提现")
    @PostMapping(value = "/saveCashData")
    public Result<?> saveCashData(TbUserCash tbUserCash) {

        return tbUserCashService.saveCashData(tbUserCash);
    }


    @AutoLog(value = "更新消息读取状态")
    @ApiOperation("更新消息读取状态")
    @PostMapping(value = "/updateUserNoticeState")
    public Result<?> updateUserNoticeState(TbUserNoticeStatus tbUserNoticeStatus) {

        return tbUserNoticeStatusService.updateUserNoticeState(tbUserNoticeStatus);
    }


    @AutoLog(value = "用户等级数据-升级条件")
    @ApiOperation("用户等级数据-升级条件")
    @PostMapping(value = "/getUserGradeData")
    public Result<?> getUserGradeData(String user_id) {

        return tbUserGameBuidingService.getUserGradeData(user_id);
    }


    @AutoLog(value = "设置头像")
    @ApiOperation("设置头像")
    @PostMapping(value = "/updateUserAvater")
    public Result<?> updateUserAvater(TbMnUser tbMnUser) {

        return tbMnUserService.updateUserAvater(tbMnUser);
    }

    @AutoLog(value = "修改用戶名")
    @ApiOperation("修改用戶名")
    @PostMapping(value = "/updateUserName")
    public Result<?> updateUserName(TbMnUser tbMnUser) {
        return tbMnUserService.updateUserName(tbMnUser);
    }


    @AutoLog(value = "绑定微信")
    @ApiOperation("绑定微信")
    @PostMapping(value = "/bindingWeixin")
    public Result<?> bindingWeixin(TbMnUser tbMnUser) {
        return tbMnUserService.bindingWeixin(tbMnUser);
    }

    @AutoLog(value = "检查旧号码")
    @ApiOperation("检查旧号码")
    @PostMapping(value = "/checkOldPhone")
    public Result<?> checkOldPhone(String user_id, String phone, String code) {

        return tbMnUserService.checkOldPhone(user_id, phone, code);
    }

    @AutoLog(value = "修改手机号码")
    @ApiOperation("修改手机号码")
    @PostMapping(value = "/updatePhone")
    public Result<?> updatePhone(String user_id, String newPhone, String code) {

        return tbMnUserService.updatePhone(user_id, newPhone, code);
    }


    @AutoLog(value = "修改个人推送状态")
    @ApiOperation("修改个人推送状态")
    @PostMapping(value = "/updatePushMsgState")
    public Result<?> updatePushMsgState(String user_id, Integer pushMsgState) {

        return tbMnUserService.updatePushMsgState(user_id, pushMsgState);
    }


    @AutoLog(value = "钱包明细")
    @ApiOperation("钱包明细")
    @PostMapping(value = "/getUserWalletDetail")
    public Result<?> getUserWalletDetail(TbUserCash tbUserCash, Integer pageNo, Integer pageSize) {

        Page<TbUserCash> page = new Page<TbUserCash>(pageNo, pageSize);
        IPage<TbUserCash> pageList = tbUserCashService.getUserCashList(page, tbUserCash);


        return Result.ok(pageList);
    }

    @AutoLog(value = "钱包信息")
    @ApiOperation("钱包信息")
    @PostMapping(value = "/getUserWallet")
    public Result<?> getUserWallet(String user_id) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("walletData", tbUserWalletService.getOneByUser_id(user_id));
        int flag = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user_id);
        TbUserAlipay tbUserAlipay = tbUserAlipayService.getOne(queryWrapper);
        if (null != tbUserAlipay) {
            flag = 1;
        }
        resultMap.put("alipayAccount_state", flag);
        resultMap.put("alipayAccount", tbUserAlipay);
        return Result.ok(resultMap);
    }


    @AutoLog(value = "SQT明细")
    @ApiOperation("SQT明细")
    @PostMapping(value = "/getSQTDetail")
    public Result<?> getSQTDetail(String user_id) {

        return tbUserSqtDetailService.getUserSQTDetail(user_id);
    }


    @AutoLog(value = "划转SQT")
    @ApiOperation("划转SQT")
    @PostMapping(value = "/transferSQT2Exchange")
    public Result<?> transferSQT2Exchange(String user_id, String uid, String transferNum) {

        return tbUserSqtService.transferSQT2Exchange(user_id, uid, transferNum);
    }

    @AutoLog(value = "支付密码确认")
    @ApiOperation("支付密码确认")
    @PostMapping(value = "/payPwdConfirm")
    public Result<?> payPwdConfirm(String user_id, String password) {

        return tbUserAlipayPwdService.payPwdConfirm(user_id, password);
    }

    @AutoLog(value = "个人中心")
    @ApiOperation("个人中心")
    @PostMapping(value = "/getPersonCenterData")
    public Result<?> getPersonCenterData(String user_id) {

        return tbMnUserService.getPersonCenterData(user_id);
    }

    @AutoLog(value = "读取新手指导")
    @ApiOperation("读取新手指导")
    @PostMapping(value = "/readNewGuide")
    public Result<?> readNewGuide(String user_id) {

        return tbMnUserService.readNewGuide(user_id);
    }

    @AutoLog(value = "收藏淘宝商品")
    @ApiOperation("收藏淘宝商品")
    @PostMapping(value = "/productCollect")
    public Result<?> productCollect(TbMnCollect tbMnCollect) {
        return tbMnCollectService.collectProduct(tbMnCollect);
    }


    @AutoLog(value = "热门收藏淘宝商品")
    @ApiOperation("热门收藏淘宝商品")
    @PostMapping(value = "/getProductCollectAll")
    public Result<?> getProductCollectAll(Integer pageNo,
                                          Integer pageSize) {
        return tbMnCollectService.getProductCollectAll(pageNo, pageSize);
    }


    @AutoLog(value = "我的收藏淘宝商品")
    @ApiOperation("我的收藏淘宝商品")
    @PostMapping(value = "/getMyProductCollectAll")
    public Result<?> getMyProductCollectAll(String user_id, Integer pageNo,
                                            Integer pageSize) throws ApiException {
        return tbMnCollectService.getMyProductCollectAll(user_id, pageNo, pageSize);
    }

    @AutoLog(value = "退出登陆")
    @ApiOperation("退出登陆")
    @PostMapping(value = "/logout")
    public Result<?> logout(String user_id) {
        return tbMnUserService.logout(user_id);

    }

    @AutoLog(value = "添加搜索历史")
    @ApiOperation("添加搜索历史")
    @PostMapping(value = "/addHistorySearch")
    public Result<?> addHistorySearch(String user_id, String serarchName) {

        return tbUserSearchHistoryService.addHistorySearch(user_id, serarchName);
    }

    @AutoLog(value = "获取搜索历史")
    @ApiOperation("获取搜索历史")
    @PostMapping(value = "/getHistorySearchList")
    public Result<?> getHistorySearchList(String user_id) {
        return tbUserSearchHistoryService.getHistorySearchList(user_id);
    }

    @AutoLog(value = "删除搜索历史")
    @ApiOperation("删除搜索历史")
    @PostMapping(value = "/deleteHistorySearchList")
    public Result<?> deleteHistorySearchList(String user_id) {

        return tbUserSearchHistoryService.deleteHistorySearchList(user_id);
    }


    @AutoLog(value = "我的邀请人")
    @ApiOperation("我的邀请人")
    @PostMapping(value = "/getMyInvitees")
    public Result<?> getMyInvitees(String user_id) {
        return tbMnUserService.getMyInvitees(user_id);
    }


    @AutoLog(value = "消息")
    @ApiOperation("消息")
    @PostMapping(value = "/getUserAllNotice1")
    public Result<?> getUserAllNotice1(TbUserNotice tbUserNotice,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {

        Page<TbUserNotice> page = new Page<TbUserNotice>(pageNo, pageSize);
        IPage<TbUserNotice> pageList = tbUserNoticeService.page(page, tbUserNotice);
        return Result.ok(pageList);

    }

    @AutoLog(value = "设置支付密码")
    @ApiOperation("设置支付密码")
    @PostMapping(value = "/setAlipayPwd")
    public Result<?> setAlipayPwd(TbUserAlipayPwd tbUserAlipayPwd) {
        return tbUserAlipayPwdService.setAlipayPwd(tbUserAlipayPwd);
    }

    @AutoLog(value = "购买积分")
    @ApiOperation("购买积分")
    @PostMapping(value = "/buyIntegral")
    public Result<?> buyIntegral(String user_id, String integral_exchange_id, Double price, int num, String return_url) {
        return tbUserIntegralService.buyIntegral(user_id, integral_exchange_id, price, num, return_url);
    }

    @AutoLog(value = "积分充值记录明细")
    @ApiOperation("积分充值记录明细")
    @PostMapping(value = "/getUserIntegralExchangeDetails")
    public Result<?> getUserIntegralExchangeDetails(TbUserIntegralDetail tbUserIntegralDetail,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {


        tbUserIntegralDetail.setFromType(TaskNamesConstant.Task_INTEGRAL_EXCHANGE);
        tbUserIntegralDetail.setState(1);
        Page<TbUserIntegralDetail> page = new Page<TbUserIntegralDetail>(pageNo, pageSize);
        IPage<TbUserIntegralDetail> pageList = tbUserIntegralDetailService.pageDatas(page, tbUserIntegralDetail);
        return Result.ok(pageList);
    }


}
